﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EarringsRecommendationWeb
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Member mem = Session["member"] as Member;

            if (mem != null)
            {
                Response.Write("<input id='hidden-email' type='hidden' value='" + mem.Email + "'>");
                Response.Write("<input id='hidden-name' type='hidden' value='" + mem.Name + "'>");
            }

            if (!string.IsNullOrEmpty(Request.QueryString["earringcode"]))
            {
                string ercode = Request.QueryString["earringcode"].ToString();

                //Earring Data
                List<EarringsMaster> earringsResult = this.GetDataEarring(ercode);
                ClientScript.RegisterArrayDeclaration("Earrings", JsonConvert.SerializeObject(earringsResult));

                //Earring Color
                List<EarringsColor> earringColor = this.GetColorEarrings(ercode);
                ClientScript.RegisterArrayDeclaration("EarringsColor", JsonConvert.SerializeObject(earringColor));

                List<RecalQty> listQty = Session["RecalQty"] as List<RecalQty>;
                int qty = 0;
                if (listQty != null)
                {
                    foreach (RecalQty item in listQty)
                    {
                        qty += Convert.ToInt32(item.Qty);
                    }
                }
                ClientScript.RegisterArrayDeclaration("qty", string.Format("\"{0}\"", qty));
            }
        }

        private List<EarringsMaster> GetDataEarring(string ercode)
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
            {
                new SqlParameter("@Ercode", ercode)
            };
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT er.Er_Code,er.Er_Name,er.Er_Detail,er.Er_Price");
            strSql.Append(" ,er.Er_Image_Front, er.Er_Image_Side, er.Er_Image_Size, er.Er_Image_Sample");
            strSql.Append(" ,erconf.Config_Name, er_Remaining");
            strSql.Append(" FROM Earrings er");
            strSql.Append(" INNER JOIN EarringsConfig erconf ON er.er_style = erconf.config_id");
            strSql.Append(" WHERE er_code = @Ercode");
            List<EarringsMaster> result = new List<EarringsMaster>();
            using (SqlDataReader value = SqlClientUtility.ExecuteReader(CommandType.Text, strSql.ToString(),sqlParams))
            {
                while (value.Read())
                {
                    string code = Convert.ToString(value["Er_Code"]);
                    string name = Convert.ToString(value["Er_Name"]);
                    string detail = Convert.ToString(value["Er_Detail"]);
                    double price = Convert.ToDouble(value["Er_Price"]);
                    string configName = Convert.ToString(value["Config_Name"]);
                    string imgFront = "Data/Earrings/" + configName + "/" + Convert.ToString(value["Er_Image_Front"]);
                    string imgSide = string.Empty;
                    string imgSize = string.Empty;
                    string imgSample = string.Empty;

                    if (!string.IsNullOrEmpty(Convert.ToString(value["Er_Image_Side"])))
                    {
                        imgSide = "Data/Earrings/" + configName + "/" + Convert.ToString(value["Er_Image_Side"]);
                    }
                    if (!string.IsNullOrEmpty(Convert.ToString(value["Er_Image_Size"])))
                    {
                        imgSize = "Data/Earrings/" + configName + "/" + Convert.ToString(value["Er_Image_Size"]);
                    }
                    if (!string.IsNullOrEmpty(Convert.ToString(value["Er_Image_Sample"])))
                    {
                        imgSample = "Data/Earrings/" + configName + "/" + Convert.ToString(value["Er_Image_Sample"]);
                    }
                    //Check Remaining
                    string status = string.Empty;
                    if (Convert.ToInt32(value["er_Remaining"]) <= 0)
                    {
                        status = GlobalValue.ERSTATUS_WAIT;
                    }
                    else
                    {
                        status = GlobalValue.ERSTATUS_READY;
                    }

                    EarringsMaster pEarrings = new EarringsMaster(code, name, detail, price, imgFront, imgSide, imgSize, imgSample, status);
                    result.Add(pEarrings);
                }
            }
            return result;
        }

        private List<EarringsColor> GetColorEarrings(string ercode)
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
            {
                new SqlParameter("@Ercode", ercode)
            };

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT erconf.config_id AS [ColorCode],erconf.Config_NameTh AS [ColorName],erconf.Image AS [ColorImgName]");
            strSql.Append(" FROM Earrings er INNER JOIN EarringsConfig erconf ON er.er_color = erconf.config_id");
            strSql.Append(" INNER JOIN EarringsModel mol ON er.er_code = mol.er_code");
            strSql.Append(" WHERE er.Er_Code IN ( SELECT DISTINCT Er_Code FROM EarringsModel");
            strSql.Append(" WHERE ModelName = (SELECT mol.ModelName FROM EarringsModel mol WHERE mol.Er_Code = @Ercode))");

            List<EarringsColor> result = new List<EarringsColor>();
            using (SqlDataReader value = SqlClientUtility.ExecuteReader(CommandType.Text, strSql.ToString(), sqlParams))
            {
                while (value.Read())
                {
                    string code = Convert.ToString(value["ColorCode"]);
                    string name = Convert.ToString(value["ColorName"]);
                    string imgPath = "Data/Color/" + Convert.ToString(value["ColorImgName"]);

                    EarringsColor pConfig = new EarringsColor(code, name, imgPath);
                    result.Add(pConfig);
                }
            }
            return result;
        }
    }
}